<?php
header("content-Type: text/html; charset=Utf-8");
ini_set('memory_limit','700M');
  $_conn=mysql_connect('localhost','root','root') or die(mysql_errno());
   mysql_select_db('mydemo',$_conn) or die(mysql_errno());
   mysql_query("SET NAMES UTF8") or die(mysql_errno());
   $d =array();
   $list=array();
   $sqlup="";
   $page = empty($_GET['page'])?1:$_GET['page'];
   $page_size = 10;
   $count =mysql_query("SELECT count(*) FROM demo ");
   $page_number = ceil($count/$page_size);
   $limit = ($page-1)*$page_size.','.$page_size;
   $sql = "SELECT * FROM demo_copy limit ".$limit;
   $row = mysql_query($sql);
	while($res = mysql_fetch_array($row))
	  {
	  	$list[]= getcontent($res['company_name'],$res['id']);
	  }
	$flag = 1;
   foreach ($list as $key => $value) {
   	       $sqlup="";
		   	if(!empty($value['company_name'])){
		   	  $sqlup = "UPDATE demo_copy SET `name`='".strip_tags($value['name'])."', phone='".strip_tags($value['phone'])."', email='".strip_tags($value['address'])."'  WHERE id='".$value['id']."'" ;
		   	  $res = mysql_query($sqlup);
		   	     if(mysql_affected_rows()>0){
				      echo $flag;
				      $flag ++;
					  }else{
					  	echo "出错";
					  }
		   	}
    }

   $page++;

	function http_get($url){
		$oCurl = curl_init();
		if(stripos($url,"http://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}

   function getcontent($name,$id){
	$url="http://www.qichacha.com/search?key=$name";
	/*$info=file_get_contents($url);
	preg_match('|<table>(.*?)<\/table>|i',$info,$m);
	print_r($m);*/
	
	$data['id'] = $id;
	$content=http_get($url);
	 $mode = "/<span class=\"font-15 text-black m-r\">([^<]+)<\/span>/";
	 $data['name']="";
	if(preg_match_all($mode,$content,$arr)){
	  $data['name']=trim($arr[0][0]); 
	 }


	   $mode = "/<span><i class=\"i i-phone3\"><\/i>([^<]+)<\/span>/";
	   $data['phone']="";
	if(preg_match_all($mode,$content,$arr)){
	  $data['phone']=trim($arr[0][0]); 
	 }

	   $mode = "/<span class=\"m-l\"><i class=\"fa  fa-envelope-o\"><\/i>([^<]+)<\/span>/";
	   $data['address'] ="";
	if(preg_match_all($mode,$content,$arr)){
	  $data['address']=trim($arr[0][0]); 
	 }

	  $mode = "/<em><em>([^<]+)<\/em><\/em>/";
	  $data['company_name'] = "";
	if(preg_match_all($mode,$content,$arr)){
	  $data['company_name']=trim($arr[0][0]); 
	 }
	 return $data;
	 }
	//生成Excel输
   	?>
 <html>
    <head>
        <title>查询公司</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div>
           <a href='./1234.php?page=<?php echo $page  ?>' >点击执行</a>
        </div>
    </body>
</html>


    



